﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyWeb.Common.Helper
{
    /// <summary>
    /// 泛型递归求树形结构
    /// </summary>
    public static class RecursionHelper<T> where T: ChildrenRoot<T>
    {

        public static void LoopToAppendChildren(List<T> all,T curItem,long pid = 0)
        {
            var subItems = all.Where(ee => ee.pid == curItem.id).ToList();
            if(subItems.Count > 0)
            {
                curItem.children = new List<T>();
                curItem.children.AddRange(subItems);
            }
            else
            {
                curItem.children = null;
            }

            foreach (var subItem in subItems)
            {
                if (subItem.id == pid && pid > 0)
                {
                    //subItem.disabled = true;//禁用当前节点
                }
                LoopToAppendChildren(all, subItem, pid);
            }
        }

        //public static void LoopToAppendChildren(List<PermissionTree> all, PermissionTree curItem, long pid, bool needbtn)
        //{

        //    var subItems = all.Where(ee => ee.pid == curItem.value).ToList();

        //    var btnItems = subItems.Where(ss => ss.isbtn == true).ToList();
        //    if (subItems.Count > 0)
        //    {
        //        curItem.btns = new List<PermissionTree>();
        //        curItem.btns.AddRange(btnItems);
        //    }
        //    else
        //    {
        //        curItem.btns = null;
        //    }

        //    if (!needbtn)
        //    {
        //        subItems = subItems.Where(ss => ss.isbtn == false).ToList();
        //    }
        //    if (subItems.Count > 0)
        //    {
        //        curItem.children = new List<PermissionTree>();
        //        curItem.children.AddRange(subItems);
        //    }
        //    else
        //    {
        //        curItem.children = null;
        //    }

        //    if (curItem.isbtn)
        //    {
        //        //curItem.label += "按钮";
        //    }

        //    foreach (var subItem in subItems)
        //    {
        //        if (subItem.value == pid && pid > 0)
        //        {
        //            //subItem.disabled = true;//禁用当前节点
        //        }
        //        LoopToAppendChildren(all, subItem, pid, needbtn);
        //    }
        //}
        //public static void LoopToAppendChildren(List<DepartmentTree> all, DepartmentTree curItem, long pid)
        //{

        //    var subItems = all.Where(ee => ee.pid == curItem.value).ToList();

        //    if (subItems.Count > 0)
        //    {
        //        curItem.children = new List<DepartmentTree>();
        //        curItem.children.AddRange(subItems);
        //    }
        //    else
        //    {
        //        curItem.children = null;
        //    }

        //    foreach (var subItem in subItems)
        //    {
        //        if (subItem.value == pid && pid > 0)
        //        {
        //            //subItem.disabled = true;//禁用当前节点
        //        }
        //        LoopToAppendChildren(all, subItem, pid);
        //    }
        //}



        //public static void LoopNaviBarAppendChildren(List<NavigationBar> all, NavigationBar curItem)
        //{

        //    var subItems = all.Where(ee => ee.pid == curItem.id).ToList();

        //    if (subItems.Count > 0)
        //    {
        //        curItem.children = new List<NavigationBar>();
        //        curItem.children.AddRange(subItems);
        //    }
        //    else
        //    {
        //        curItem.children = null;
        //    }


        //    foreach (var subItem in subItems)
        //    {
        //        LoopNaviBarAppendChildren(all, subItem);
        //    }
        //}



        //public static void LoopToAppendChildrenT<T>(List<T> all, T curItem, string parentIdName = "Pid", string idName = "value", string childrenName = "children")
        //{
        //    var subItems = all.Where(ee => ee.GetType().GetProperty(parentIdName).GetValue(ee, null).ToString() == curItem.GetType().GetProperty(idName).GetValue(curItem, null).ToString()).ToList();

        //    if (subItems.Count > 0) curItem.GetType().GetField(childrenName).SetValue(curItem, subItems);
        //    foreach (var subItem in subItems)
        //    {
        //        LoopToAppendChildrenT(all, subItem);
        //    }
        //}
    }
    /// <summary>
    /// 子项
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ChildrenRoot<T>
    {
        /// <summary>
        /// Id
        /// </summary>
        public long id { get; set; }
        /// <summary>
        /// 父Id
        /// </summary>
        public long pid { get; set; }
        /// <summary>
        /// 子项
        /// </summary>
        public List<T>? children { get; set; }
    }

    /// <summary>
    /// 菜单数
    /// </summary>
    public class PermissionTree:ChildrenRoot<PermissionTree>
    {
        public string label { get; set; } = string.Empty;
        public int order { get; set; }
        /// <summary>
        /// 菜单类型（1-菜单，2-按钮，3-搜索框）
        /// </summary>
        public int type { get; set; } = 1;
        /// <summary>
        /// 是否启用
        /// </summary>
        public bool? disabled { get; set; }
        public List<PermissionTree>? btns { get; set; }
    }
    /// <summary>
    /// 部门数
    /// </summary>
    public class DepartmentTree:ChildrenRoot<DepartmentTree>
    {
        public long value { get; set; }
        //public long Pid { get; set; }
        public string label { get; set; } = string.Empty;
        public int order { get; set; }
        public bool disabled { get; set; }
        //public List<DepartmentTree> children { get; set; }
    }

    /// <summary>
    /// 导航栏路由数
    /// </summary>
    public class NavigationBar: ChildrenRoot<NavigationBar>
    {
        //public long id { get; set; }
        /// <summary>
        /// 菜单执行Action名
        /// </summary>
        public string code { get; set; } = string.Empty;
        public int order { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string name { get; set; } = string.Empty;
        /// <summary>
        /// 是否隐藏
        /// </summary>
        public bool? IsHide { get; set; } = false;
        /// <summary>
        /// 菜单类型（1-菜单，2-按钮，3-搜索框）
        /// </summary>
        public int Type { get; set; } = 1;
        /// <summary>
        /// 路由路径
        /// </summary>
        public string path { get; set; } = string.Empty;
        /// <summary>
        /// 执行方法
        /// </summary>
        public string Func { get; set; } = string.Empty;
        /// <summary>
        /// 图标
        /// </summary>
        public string iconCls { get; set; } = string.Empty;
        public NavigationBarMeta? meta { get; set; }
        //public List<NavigationBar> children { get; set; }
    }

    public class NavigationBarMeta
    {
        /// <summary>
        /// 标题
        /// </summary>
        public string title { get; set; } = string.Empty;
        public bool requireAuth { get; set; } = true;
        public bool? NoTabPage { get; set; } = false;
        public bool? keepAlive { get; set; } = false;


    }
}
